package ui;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JScrollPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JButton;
import bot.ScriptMethods;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
import java.util.Vector;

public class InfoUI extends JPanel {

	private final Vector<String> usernames = new Vector<String>();
	private final Vector<String> hitpoints = new Vector<String>();
	private final Vector<String> pids = new Vector<String>();

	private ScriptMethods scriptMethods;
	private JList hpList;
	private JList hpList2;
	private JList hpList3;
	private JFrame frame1;

	public InfoUI() {
		this.scriptMethods = new ScriptMethods();
		initComponents();
		frame1.setVisible(true);
	}

	@SuppressWarnings("deprecation")
	private void initComponents() {
		frame1 = new JFrame();
		final JLabel label1 = new JLabel();
		final JLabel label2 = new JLabel();
		final JLabel label4 = new JLabel();
		final JLabel label5 = new JLabel();
		final JLabel label6 = new JLabel();
		JLabel labelUser = new JLabel();
		JLabel labelHp = new JLabel();
		JLabel labelP = new JLabel();
		hpList = new JList();
		hpList2 = new JList();
		hpList3 = new JList();

		getNames();

		frame1.setTitle("ARS");
		Container container = frame1.getContentPane();
		container.setLayout(null);
		container.setPreferredSize(new Dimension(450, 160));

		label4.setText("Hits: " + scriptMethods.getCurrentLevel()[3] + "/"
				+ scriptMethods.getMaxLevel()[3]);
		container.add(label4);
		label4.setBounds(new Rectangle(new Point(10, 3), new Dimension(100, 15)));

		label5.setText("Prayer: " + scriptMethods.getCurrentLevel()[5] + "/"
				+ scriptMethods.getMaxLevel()[5]);
		container.add(label5);
		label5.setBounds(new Rectangle(new Point(10, 20),
				new Dimension(100, 15)));

		try {
			label6.setText("Fatigue: " + scriptMethods.getFatigue() + "%");
			container.add(label6);
			label6.setBounds(new Rectangle(new Point(10, 37), new Dimension(100, 15)));
		} catch (Exception e) { }

		label1.setText("X: " + scriptMethods.ourPlayerX());
		container.add(label1);
		label1.setBounds(new Rectangle(new Point(10, 54),
				new Dimension(100, 15)));

		label2.setText("Y: " + scriptMethods.ourPlayerY());
		container.add(label2);
		label2.setBounds(new Rectangle(new Point(10, 71),
				new Dimension(100, 15)));

		labelUser.setText("Username");
		container.add(labelUser);
		labelUser.setBounds(new Rectangle(new Point(90, 3),
				new Dimension(100, 15)));

		JScrollPane hpScroll = new JScrollPane(hpList);
		hpScroll.setBounds(new Rectangle(new Point(90, 20), new Dimension(180, 144)));
		container.add(hpScroll);

		labelHp.setText("Hitpoints");
		container.add(labelHp);
		labelHp.setBounds(new Rectangle(new Point(274, 3),
				new Dimension(100, 15)));

		JScrollPane hpScroll2 = new JScrollPane(hpList2);
		hpScroll2.setBounds(new Rectangle(new Point(274, 20), new Dimension(90, 144)));
		container.add(hpScroll2);

		labelP.setText("Player Index");
		container.add(labelP);
		labelP.setBounds(new Rectangle(new Point(368, 3),
				new Dimension(100, 15)));

		JScrollPane hpScroll3 = new JScrollPane(hpList3);
		hpScroll3.setBounds(new Rectangle(new Point(368, 20), new Dimension(90, 144)));
		container.add(hpScroll3);

		JButton refresh = new JButton();
		refresh.setBounds(new Rectangle(new Point(10, 140), new Dimension(60, 23)));
		refresh.setText("Refresh");
		refresh.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent actionEvent) {
				getNames();
			}
		});
		container.add(refresh);

		frame1.pack();
		frame1.setResizable(false);
		frame1.setLocationRelativeTo(null);

		final Thread thread = new Thread(new Runnable() {
			public void run() {
				while (true) {
					label1.setText("X: " + scriptMethods.ourPlayerX());
					label2.setText("Y: " + scriptMethods.ourPlayerY());
					label4.setText("Hits: "
							+ scriptMethods.getCurrentLevel()[3] + "/"
							+ scriptMethods.getMaxLevel()[3]);
					label5.setText("Prayer: "
							+ scriptMethods.getCurrentLevel()[5] + "/"
							+ scriptMethods.getMaxLevel()[5]);
					try {
						label6.setText("Fatigue: " + scriptMethods.getFatigue() + "%");
					} catch (Exception e) { }
					try {
						Thread.sleep(1000);
					} catch (Exception e) {
					}
				}
			}
		});

		frame1.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				thread.stop();
				System.gc();
			}
		});

		thread.start();
	}


	public void getNames() {
		usernames.clear();
		hitpoints.clear();
		pids.clear();
		try {
			for (int i = 0; i < scriptMethods.getPlayerCount(); i++) {
				try {
					hitpoints.add(Integer.toString(scriptMethods.getTotalHp(i)));
					usernames.add(scriptMethods.getUsername(i));
					pids.add(Integer.toString(scriptMethods.getServerIndex(i)));

				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}
			hpList.setListData(usernames);
			hpList2.setListData(hitpoints);
			hpList3.setListData(pids);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}